package com.xj.until;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
public class MailTestController {

    @Autowired
    private MailUtil mailUtil;


//    @Resource
//    private JwtAuthController jwtAuthController;

    /**
     * 测试一个简单的、由自己撰写邮件标题和内容的邮件
     **/
    public Result sendSimpleMail(String email, String title, String content) {
        mailUtil.sendSimpleMail(email,title,content);
        return Result.ok("发送成功");
    }

    /**
     * 测试一个简单的html邮件
     **/


    public String sendHtmlMail(String email) throws MessagingException {
        //User user =(User)(SecurityContextHolder.getContext().getAuthentication().getPrincipal());
       // String name = user.getName();
//        JwtAuthController jwtAuthController=new JwtAuthController();
//
//        //User user = jwtAuthController.ccc();
//
//        System.out.println("jwt封装名字："+user.getName());
//        //String email = user.getEmail();
//        System.out.println("jwt封装邮箱："+ user.getEmail());
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request1 = attributes.getRequest();
        String x1="<html>\n"+"<body>\n" +
                "    <div style=\"width: 100vw;height: 80vh;display: flex;justify-content: center;align-items: center;\">\n" +
                "        <div style=\"width: 800px;height: 100px;box-shadow: -8px -8px 15px rgba(0, 0, 0, 0.1),8px 8px 15px rgba(0, 0, 0, 0.1);border-radius: 20px;padding: 20px;\">\n" +
                "            <div style=\"height: 20%;border-bottom: 1px solid rgba(0, 0, 0, 0.3);color:rgba(0, 0, 0, 0.6);line-height: 10vh;font-size: 30px;padding-left: 20px;\">\n" +
                "                账号登录提醒</div>\n" +
                "            <div style=\"height: 10%;padding: 20px; color:rgba(0, 0, 0, 0.6);\">\n" +
//                "                <h3 style=\"padding: 10px;\">"+user.getName()+"，你好：</h3>\n" +
//                "                <p style=\"padding: 10px;\">你的账号于 "+now()+"登录了广东省绩效系统，在IP地址为"+user.getIp11()+"(太原)的设备登录,如非本人操作，建议尽快修改账户密码</p>\n" +
                "                <p style=\"padding: 10px;\">请关注广东省官博获取更多信息，使用政府绩效系统更快更准更安全！[本邮件由系统自动发送，请勿直接回复]</p>\n" +
                "            </div>\n" +
                "        </div>\n" +
                "    </div>\n" +
                "</body>"+ "</html>\n";

        mailUtil.sendHtmlMail(email,"账号登录提醒",x1);
        return "发送成功";
    }



    public String sendHtmlMail111(String password,String email1) throws MessagingException {
        String x1="<html>\n"+"<body>\n" +
                "    <div style=\"width: 100vw;height: 80vh;display: flex;justify-content: center;align-items: center;\">\n" +
                "        <div style=\"width: 800px;height: 100px;box-shadow: -8px -8px 15px rgba(0, 0, 0, 0.1),8px 8px 15px rgba(0, 0, 0, 0.1);border-radius: 20px;padding: 20px;\">\n" +
                "            <div style=\"height: 20%;border-bottom: 1px solid rgba(0, 0, 0, 0.3);color:rgba(0, 0, 0, 0.6);line-height: 10vh;font-size: 30px;padding-left: 20px;\">\n" +
                "                账号登录提醒</div>\n" +
                "            <div style=\"height: 10%;padding: 20px; color:rgba(0, 0, 0, 0.6);\">\n" +
                "                <h3 style=\"padding: 10px;\">你好：</h3>\n" +
                "                <p style=\"padding: 10px;\">你的账号于 "+now()+":重置了密码，您的密码为\n"+password +
                "                <p style=\"padding: 10px;\">请关注广东省官博获取更多信息，使用政府绩效系统更快更准更安全！[本邮件由系统自动发送，请勿直接回复]</p>\n" +
                "            </div>\n" +
                "        </div>\n" +
                "    </div>\n" +
                "</body>"+ "</html>\n";

        mailUtil.sendHtmlMail(email1,"修改密码",x1);
        return "发送成功";
    }

    public String sendHtmlMail1(String num,String email1) throws MessagingException {
//        User user =(User)(SecurityContextHolder.getContext().getAuthentication().getPrincipal());
//        String name = user.getName();
//        System.out.println("jwt封装名字："+name);
        // String email = user.getEmail();
        // System.out.println("jwt封装邮箱："+email);
        String x1="<html>\n"+"<body>\n" +
                "    <div style=\"width: 100vw;height: 80vh;display: flex;justify-content: center;align-items: center;\">\n" +
                "        <div style=\"width: 800px;height: 800px;box-shadow: -8px -8px 15px rgba(0, 0, 0, 0.1),8px 8px 15px rgba(0, 0, 0, 0.1);border-radius: 20px;padding: 20px;\">\n" +
                "            <div style=\"height: 20%;border-bottom: 1px solid rgba(0, 0, 0, 0.3);color:rgba(0, 0, 0, 0.6);line-height: 10vh;font-size: 30px;padding-left: 20px;\">\n" +
                "                验证码</div>\n" +
                "            <div style=\"height: 10%;padding: 20px; color:rgba(0, 0, 0, 0.6);\">\n" +
//                "                <h3 style=\"padding: 10px;\">"+name+"，你好：</h3>\n" +
                "                <p style=\"padding: 10px;\">\n【政府绩效】验证码:"+num+"，您正在进行邮箱绑定校验，切勿将验证码泄露于他人，验证码5分钟内有效。</p>\n" +
                "                <p style=\"padding: 10px;\">请关注广东省官博获取更多信息，使用政府绩效系统更快更准更安全！[本邮件由系统自动发送，请勿直接回复]</p>\n" +
                "            </div>\n" +
                "        </div>\n" +
                "    </div>\n" +
                "</body>"+ "</html>\n";

        mailUtil.sendHtmlMail(email1,"验证码",x1);
        return "发送成功";
    }


    private String now() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        String formattedDateTime = now.format(formatter);
        System.out.println("Current time: " + formattedDateTime);
        return formattedDateTime;
    }

    /**
     * 测试文档之类附件传输的邮件
     **/

    public String sendAttachmentsMail() throws MessagingException {
        String filePath = "src/main/resources/static/zz.docx";
        String content = "<html>\n" +
                "<body>\n" +
                "<h3>hello world</h3>\n" +
                "<h1>html</h1>\n" +
                "<h1>附件传输</h1>\n" +
                "<body>\n" +
                "</html>\n";
        mailUtil.sendAttachmentsMail("1783406930@qq.com","附件传输",content, filePath);
        return "发送成功";
    }

    /**
     * 测试图片之类的、且显示在邮件内容上的邮件
     **/

    public String sendInlinkResourceMail() throws MessagingException {
        //TODO 相对路径
        String imgPath = "src/main/resources/static/test01.png";
        String rscId = "test";
        String content = "<html>" +
                "<body>" +
                "<h3>hello world</h3>" +
                "<h1>html</h1>" +
                "<h1>图片邮件</h1>" +
                "<img src='cid:"+rscId+"'></img>" +
                "<body>" +
                "</html>";

        mailUtil.sendInlinkResourceMail("1783406930@qq.com","这是一封图片邮件",content, imgPath, rscId);
        return "发送成功";
    }

    /**
     * 测试一个HTML模板的邮件
     **/

//    public String testTemplateMailTest() throws MessagingException {
//        Context context = new Context();
//        context.setVariable("id","liuxing121380110");
//
//        String emailContent = templateEngine.process("emailTemplate", context);
//        mailUtil.sendHtmlMail("1783406930@qq.com","这是一封HTML模板邮件",emailContent);
//        return "发送成功";
//
//    }
}
